//https://leetcode.cn/problems/flood-fill/
class Solution {
    int[] dx, dy;
    int[][] image;
    int color;
    int n, m;
    int val;
    public int[][] floodFill(int[][] image_, int sr, int sc, int color_) {
        if(color_ == image_[sr][sc]) return image_;
        image = image_;
        val = image[sr][sc];
        color = color_;
        n = image.length;
        m = image[0].length;
        dx = new int[]{-1, 1, 0, 0};
        dy = new int[]{0, 0, -1, 1};
        image[sr][sc] = color;
        dfs(sr, sc);
        return image;
    }
    public void dfs(int i, int j) {
        for(int k = 0; k < 4; k++) {
            int x = i + dx[k];
            int y = j + dy[k];
            if(x >= 0 && x < n && y >= 0 && y < m && image[x][y] == val) {
                image[x][y] = color;
                dfs(x, y);
            }
        }
    }
}